package com.sc.nft.mapper;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.entity.SecondOrder;
import com.sc.nft.entity.dto.*;
import com.sc.nft.entity.vo.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 二级订单表(sc_second_order)数据Mapper
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-10-05 12:53:07
 */
@Mapper
public interface SecondOrderMapper extends BaseMapper<SecondOrder> {

//    Page<SecondOrderPageDTO> orderPage(IPage page, @Param("userId") Long userId);

//    Page<SecondOrderPageDTO> orderPageBySalesUser(Page page, @Param("userId") Long userId);

//    SecondOrderDetailDTO orderDetail(@Param("id") Long id, @Param("userId") Long userId);

//    SecondOrderDetailDTO OrderDetailBySalesUserId(@Param("id") Long id, @Param("userId") Long userId);

//    /**
//     * 成交详情
//     *
//     * @param id
//     * @param userId
//     * @return
//     */
//    SecondOrderDetailBySalesUserDTO OrderDetailBySalesUser(@Param("id") Long id, @Param("userId") Long userId);

//    Page<SecondOrderDTO> adminPage(IPage page, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("buyUserTel") String buyUserTel, @Param("saleUserTel") String saleUserTel, @Param("marketType") Integer marketType, @Param("firstTitle") String firstTitle);

    /**
     * 根据交易类型获取藏品名称列表
     *
     * @param marketType
     * @return
     */
    List<SecondOrderCollectionListDTO> collectionList(@Param("marketType") Integer marketType);

    /**
     * 根据交易时间，交易类型，藏品id获取交易列表
     *
     * @param collectionIdList
     * @param marketType
     * @param startTime
     * @param endTime
     * @return
     */
    @Deprecated
    List<SecondOrderExportListVO> getListByCollectionIdAndTime(@Param("collectionIdList") List<Long> collectionIdList, @Param("marketType") Integer marketType, @Param("startTime") Date startTime, @Param("endTime") Date endTime);


    /**
     * 查询成功的订单最高成交价
     *
     * @param collectionId
     * @return
     */
    @Deprecated
    BigDecimal getMaxPriceByCollectionId(@Param("collectionId") Long collectionId);

    /**
     * 查询成功的订单最新成交价
     *
     * @param collectionId
     * @return
     */
    @Deprecated
    BigDecimal getLastPrice(@Param("collectionId") Long collectionId);

    /**
     * @param collectionId
     * @return
     */
    SecondOrder selectByRelationIdSubOneDayData(@Param("collectionId") Long collectionId);


    SecondOrder selectBySecondCollectionId(@Param("secondCollectionId") Long secondCollectionId);


//    /**
//     * 获取用户支付的时候所需要的订单数据
//     *
//     * @return
//     */
//    List<SecondOrder> selectNewSecondCollectionPayOrder(@Param("list") List<Long> list, @Param("type") Integer type, @Param("userId") Long userId);

    /**
     * 寄售藏品历史折线图
     *
     * @param collectionId
     * @return
     */
    List<BrandMarketHistoryDetailsVO> brandMarketHistoryDetails(@Param("collectionId") Long collectionId);

    /**
     * 新二级商品批量购买的时候的购买列表
     *
     * @param newSecondCollectionGoodsBuyListDTO
     * @return
     */
    List<NewSecondCollectionGoodsBuyListVO> selectNewSecondCollectionGoodsBuyList(@Param("newSecondCollectionGoodsBuyListDTO") NewSecondCollectionGoodsBuyListDTO newSecondCollectionGoodsBuyListDTO, @Param("userId") Long userId, @Param("maxId") Long maxId);

    List<NewSecondCollectionGoodsBuyListVO> selectNewSecondCollectionGoodsBuyIds(@Param("collectionId") Long collectionId, @Param("ids") List<Long> ids, @Param("userId") Long userId, @Param("size") Integer size, @Param("maxId") Long maxId);

    /**
     * 获取用户卖出的订单
     *
     * @param page
     * @param newSecondCollectionGoodsBuyOrderListDTO
     * @param userId
     * @return
     */
    @Deprecated
    Page<NewSecondCollectionGoodsBuyOrderListVO> selectNewSecondCollectionGoodsSaleOrderList(Page page, @Param("data") NewSecondCollectionGoodsBuyOrderListDTO newSecondCollectionGoodsBuyOrderListDTO, @Param("userId") Long userId);

    /**
     * 获取用户买入订单
     *
     * @param page
     * @param newSecondCollectionGoodsBuyOrderListDTO
     * @param userId
     * @return
     */
    @Deprecated
    Page<NewSecondCollectionGoodsBuyOrderListVO> selectNewSecondCollectionGoodsBuyOrderList(Page page, @Param("data") NewSecondCollectionGoodsBuyOrderListDTO newSecondCollectionGoodsBuyOrderListDTO, @Param("userId") Long userId);

    /**
     * 获取用户过期订单
     *
     * @param page
     * @param userId
     * @return
     */
    @Deprecated
    Page<NewSecondCollectionGoodsBuyOrderListVO> overdueOrderList(Page page, @Param("userId") Long userId);

    /**
     * 获取用户最新未付款订单.
     *
     * @param userId
     * @return
     */
    SecondOrder getUserInfoNewOrder(@Param("userId") Long userId);

    /**
     * 获取指定的藏品未支付订单
     *
     * @param userId
     * @param collectionId
     * @return
     */
    List<NewSecondCollectionNotPayOrderVO> getNewSecondCollectionNotPayOrder(@Param("userId") Long userId, @Param("collectionId") Long collectionId);

    NewSecondCollectionOrderDetailsVO nweSecondCollectionOrderDetails(@Param("orderId") Long orderId, @Param("userId") Long userId);

    NewSecondCollectionOrderDetailsVO nweSecondCollectionOrderSaleDetails(@Param("orderId") Long orderId, @Param("userId") Long userId);

    SecondOrder getOrderBeforeDayLastDealData(@Param("collectionId") Long collectionId);

//    /**
//     * 获取可以购买的数量
//     *
//     * @param collectionId
//     * @param type
//     * @return
//     */
//    Integer purchasableCount(@Param("collectionId") Long collectionId, @Param("type") Integer type);


    /**
     * 获取超时订单id
     *
     * @return
     */
    List<SecondOrder> getPreTimeOutOrderIds(@Param("time") Date time);


//    List<Long> userBlackList();

    /**
     * 查询当前寄售藏品是否存在锁单
     *
     * @param secondCollectionId
     * @return
     */
    SecondOrder isLockOrderBySecondCollectionId(@Param("secondCollectionId") Long secondCollectionId);


    /**
     * 获取今天第一笔订单
     *
     * @return
     */
    SecondOrder toDayOrderLimitOneAsc();

//    /**
//     * 查询该藏品前一天是否有出售的数量
//     *
//     * @param collectionId
//     * @return
//     */
//    Integer getCountByCollectionId(@Param("collectionId") Long collectionId);


//    /**
//     * 二级订单锁单数量
//     *
//     * @param userId
//     * @return
//     */
//    Integer getUnfreezeCount(@Param("userId") Long userId);

    /**
     * 获取用户当天被锁数据
     *
     * @param userId
     * @return
     */
    AdminSecondOrderNowDataLockVO nowDataLockOrderSize(@Param("userId") Long userId);

//    /**
//     * 获取2级订单没有结算的订单
//     *
//     * @return
//     */
//    List<SecondOrder> secondOrderRemit();

    /**
     * 二级市场的手续总额
     *
     * @return
     */
    BigDecimal getSumInCharge();

    /**
     * 获取昨日二级市场销售总额
     *
     * @return
     */
    BigDecimal getToDaySecondSellAmount();

//    BigDecimal getToDaySecondSellAmountByDay(@Param("day") String day);

    /**
     * 获取二级市场的销售额 并且过滤市值账号
     *
     * @param type
     * @param ids
     * @return
     */
    BigDecimal getSecondSellAmount(@Param("type") Integer type, @Param("ids") List<Long> ids);

//    BigDecimal getSecondSellAmountByDay(@Param("type") Integer type, @Param("ids") List<Long> ids, @Param("day") String day);

    /**
     * 获取二级市场的销售额 市值账号购买
     *
     * @param type
     * @param ids
     * @return
     */
    @Deprecated
    BigDecimal getSecondSellAmountByMarketAccount(@Param("type") Integer type, @Param("ids") List<Long> ids);

//    BigDecimal getSecondSellAmountByMarketAccountByDay(@Param("type") Integer type, @Param("ids") List<Long> ids, @Param("day") String day);

    /**
     * 获取昨日二级市场 交易数量
     *
     * @param marketType
     * @return
     */
    Integer getToDaySellCountByType(@Param("marketType") Integer marketType);

//    Integer getToDaySellCountByTypeByDay(@Param("marketType") Integer marketType, @Param("day") String day);

    /**
     * 获取昨日二级市场 交易金额
     *
     * @param marketType
     * @return
     */
    BigDecimal getToDaySellAmount(@Param("marketType") Integer marketType);

//    BigDecimal getToDaySellAmountByDay(@Param("marketType") Integer marketType, @Param("day") String day);

    /**
     * 获取昨日二级市场 买家
     *
     * @param marketType
     * @return
     */
    Integer getToDaySellBuyUserCount(@Param("marketType") Integer marketType);

//    Integer getToDaySellBuyUserCountByDay(@Param("marketType") Integer marketType, @Param("day") String day);

    /**
     * 获取昨日二级市场 卖家
     *
     * @param marketType
     * @return
     */
    Integer getToDaySellUserCount(@Param("marketType") Integer marketType);

//    Integer getToDaySellUserCountByDay(@Param("marketType") Integer marketType, @Param("day") String day);

    /**
     * 获取昨日某个类型的所有订单
     *
     * @param marketType
     * @return
     */
    List<SecondOrder> getOrderListByMarketType(@Param("marketType") Integer marketType);

//    List<SecondOrder> getOrderListByMarketTypeByDay(@Param("marketType") Integer marketType, @Param("day") String day);

    /**
     * 获取昨日某个类型 红包抵扣金额
     *
     * @param marketType
     * @return
     */
    BigDecimal getToDayBuyPayBalancePriceByType(@Param("marketType") Integer marketType);

//    BigDecimal getToDayBuyPayBalancePriceByTypeByDay(@Param("marketType") Integer marketType, @Param("day") String day);


    /**
     * 获取昨日某个类型 交易手续费
     *
     * @param marketType
     * @return
     */
    BigDecimal getToDaySumCharge(@Param("type") Integer type, @Param("marketType") Integer marketType, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    Integer getMarketUserCount(@Param("marketType") Integer marketType, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    /**
     * @param type
     * @param marketType
     * @param startTime
     * @param endTime
     * @return
     */
    BigDecimal getMonthlyStatistics(@Param("type") Integer type, @Param("marketType") Integer marketType, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    /**
     * 今日最高价和最低价
     *
     * @param id
     */
    TodaySHighAndLowPricesVO todaySHighAndLowPrices(@Param("id") Long id);
    TodaySHighAndLowPricesVO todaySHighAndLowPricesForConsensus(@Param("id") Long id);
    TodaySHighAndLowPricesVO todaySHighAndLowPricesNotInConsensus(@Param("id") Long id);

    /**
     * 获取自由市场最新成交价
     *
     * @param id
     * @return
     */
    BigDecimal latestTransactionPrice(@Param("id") Long id);
    BigDecimal latestTransactionPriceForConsensus(@Param("id") Long id);
    BigDecimal latestTransactionPriceNotInConsensus(@Param("id") Long id);

    /**
     * 获取自由市场成交量
     *
     * @param id
     * @return
     */
    int dailyTurnover(@Param("id") Long id);
    int dailyTurnoverForConsensus(@Param("id") Long id);
    int dailyTurnoverNotInConsensus(@Param("id") Long id);

    /**
     * 获取自由市场流转记录
     *
     * @param toMp
     * @param flowRecordDTO
     * @return
     */
    Page<FlowRecordVO> flowRecord(Page toMp, @Param("flowRecordDTO") FlowRecordDTO flowRecordDTO);

    @Deprecated
    Page<FlowRecordVO> flowRecordForConsensus(Page toMp, @Param("flowRecordDTO") FlowRecordDTO flowRecordDTO);
    @Deprecated
    Page<FlowRecordVO> flowRecordNotInConsensus(Page toMp, @Param("flowRecordDTO") FlowRecordDTO flowRecordDTO);


    List<DingLianDealPracticalVO> dealPracticalNotInConsensus();
    List<DingLianDealPracticalVO> dealPracticalInConsensus();

    List<DingLianTransactionRecordVO> transactionRecord(@Param("dto") DingLianTransactionRecordDTO dto);

    Page<CollectionSimpleVO> getPageSecondaryMarketCollection(Page page);

    /**
     * 盯链 寄售记录
     *
     * @param id
     * @param limit
     * @return
     */
    List<ConsignmentRecordVO> consignmentRecord(@Param("id") Long id, @Param("limit") Integer limit);
    List<ConsignmentRecordVO> consignmentRecordForConsensus(@Param("id") Long id, @Param("limit") Integer limit);
    Integer idCountPage(@Param("buyUserId") Long buyUserId, @Param("saleUserId")Long saleUserId, @Param("collectionId")Long collectionId, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("marketType") Integer marketType);

    Page<Long> idPage(@Param("page") Page page, @Param("buyUserId") Long buyUserId, @Param("saleUserId") Long saleUserId, @Param("collectionId") Long collectionId, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("marketType") Integer marketType);

    List<SecondOrderDTO> adminPageByIdList(@Param("idList") List<Long> ids);

    List<ZhiTanTradeVO> tradeList(@Param("idList") List<Long> idList);

    List<Long> tradeIdList(@Param("date") DateTime date, @Param("limit") Integer limit);

//    @Select("select * from sc_second_order where is_delete=0 and status=2 and pay_time>'2023-12-18 00:00:00' and is_team_income=1 order by id ")
//    List<SecondOrder> initTmp();

//    @Select("select * from sc_second_order where is_delete=0 and status=2 and create_time between #{startTime} and #{endTime} and id>7611866  and is_team_income=1")
//    List<SecondOrder> initTmp2(@Param("startTime") Date start,@Param("endTime") Date endTime);

    /**
     * 市场搜索
     *
     * @param request
     * @param mp
     * @return
     */
    IPage<SecondaryMarketSearchVO> marketSearch(@Param("request") SecondaryMarketSearchDTO request, Page mp);

    /**
     * 二级过期订单详情
     *
     * @param orderId
     * @return
     */
    SecondOverdueOrderDetailsVO overdueOrderDetails(@Param("orderId") Long orderId, @Param("userId") Long userId);

    /**
     * 通过ID区间导出数据（非业务接口），注：包含逻辑删除数据
     *
     * @param begin 起始ID
     * @param end   结束ID
     * @return 数据列表
     */
    @Select("SELECT * FROM sc_second_order WHERE id BETWEEN #{begin} AND #{end} ORDER BY id ASC")
    List<SecondOrder> export(@Param("begin") Long begin, @Param("end") Long end);

    @Select("SELECT MAX(id) FROM sc_second_order")
    long getMaxId();

}
